package com.sec.etech.ism.service.impl;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sec.etech.ism.mapper.PreProjectFromCrmMapper;
import com.sec.etech.ism.model.PreProjectFromCrm;
import com.sec.etech.ism.service.IsmMethodService;
import lombok.extern.slf4j.Slf4j;
import org.openbpm.base.api.exception.BusinessException;
import org.openbpm.base.core.util.StringUtil;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Service
public class IsmCpmNotifyServiceImpl implements IsmMethodService {

    @Resource
    PreProjectFromCrmMapper preProjectFromCrmMapper;

    @Override
    public JSONObject call(String sysCode, String serviceCode, JSONObject businessParams) throws Exception {

        JSONObject result = new JSONObject();

        String cate = businessParams.getJSONObject("input_params").getString("type");
        String id = businessParams.getJSONObject("input_params").getString("id");
        result.put("data", businessParams);
        result.put("flag", "success");
        businessParams.put("greet", "Hello,from ETech!");

//        if (true){
//            return result ;
//        }

        if (StringUtil.isEmpty(cate)){
            throw new BusinessException("缺少必要参数type,可填写contract或opportunity!");
        }
        if (StringUtil.isEmpty(id)){
            throw new BusinessException("缺少必要参数id!");
        }

        String data = "";

        if (cate.equals("contract")){
            String token = getToken();
            data = getContract(id, token);
        }else if (cate.equals("opportunity")){
            String token = getToken();
            data = getProject(id, token);
        } else {
            throw new BusinessException("必要参数type填写错误,可填写contract或opportunity!");
        }

        PreProjectFromCrm crm = new PreProjectFromCrm();

        crm.setCrmid(id);
        crm.setCate(cate);
        crm.setResponse(data);
        crm.setPostdate(new Date());
        preProjectFromCrmMapper.insert(crm);

        return result;
    }


    public String getProject(String projectid, String token){
        HttpResponse response = HttpUtil.createGet("https://api-mercury.shanghai-electric.com/rest/data/v2.0/xobjects/opportunity/" + projectid)
                .header("Authorization", "Bearer " + token)
                .execute();
        return response.body();
    }

    public String getContract(String contractid, String token){
        HttpResponse response = HttpUtil.createGet("https://api-mercury.shanghai-electric.com/rest/data/v2.0/xobjects/contract/" + contractid)
                .header("Authorization", "Bearer " + token)
                .execute();
        return response.body();
    }

    @Cacheable("crmToken")
    public String getToken(){
        HttpResponse response = HttpUtil.createPost("https://api-mercury.shanghai-electric.com/oauth2/token")
                .header("Content-Type", "application/x-www-form-urlencoded")
                .form("grant_type", "password")
                .form("client_id", "21b3494f548b22b646ef1c7a19ed9626")
                .form("client_secret", "c4c9e5a96d660d09a369da2fbf796b89")
                .form("redirect_uri", "https://api-mercury.shanghai-electric.com")
                .form("username", "guanhui@shanghai-electric.com")
                .form("password", "Sedt!!111Jdvm3cAyh9")
                .execute();
        JSONObject resp = JSONObject.parseObject(response.body());
        return resp.getString("access_token");
    }


//    public static void main(String[] args) {
//        String token = "b04442a7cf29da5351ca076d1485538fbae3b2d075df5936bd00ea09d1d14873.MTE0OTg3MjQyNzYzMjMyNQ==0";
//        getContract("2532599094494918", token);
//        getProject("2540415627675265", token);
//    }

    @Override
    public String methodName() {
        return "cpmNotify";
    }
}
